<template>
  <div>
    <div class="head">
      <van-nav-bar>
        <template #left>
          <div class="logo">
            <img src="@/assets/images/home_active.png" alt="" />
            <span class="logo-title">Luckin Coffee</span>
          </div>
        </template>
        <template #right>
          <div class="goBefore" @click="methods.goLook">先逛一逛</div>
        </template>
      </van-nav-bar>
    </div>
    <div class="login-box">
      <h1 class="login-title">找回密码！</h1>
      <p class="login-subTitle">Find password</p>
      <van-form>
        <van-field
          v-model="mailbox"
          name="邮箱"
          label="邮箱"
          placeholder="邮箱"
          :rules="[
            {
              required: true,
              message: '请填写正确邮箱',
              pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
            }
          ]"
        />
        <van-field
          v-model="sms"
          center
          clearable
          type="text"
          name="验证码"
          label="验证码"
          placeholder="验证码(6个字符)"
        >
          <template #button>
            <van-button
              size="small"
              type="primary"
              color="rgb(12,52,186)"
              :disabled="isClick"
              @click="methods.onSendCode"
            >
              {{ isClick ? num : '发送验证码' }}
            </van-button>
          </template>
        </van-field>
        <div class="login-tip">
          <span @click="methods.goLogin">已有账号,立即登录</span>
        </div>
        <div style="margin: 16px">
          <van-button
            round
            block
            type="primary"
            native-type="submit"
            color="rgb(12,52,186)"
            @click="methods.next"
          >
            下一步
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { postFormData } from '@/api'
import { showSuccessToast, showToast } from 'vant'
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const sms = ref('')
const mailbox = ref('')
const isClick = ref(false)
const num = ref(60)

const methods = {
  next() {
    const validCode = sms.value

    postFormData({
      url: '/auth/password/code/verify',
      params: {
        email: mailbox.value,
        code: validCode
      }
    })
      .then((res) => {
        if (res.data) {
          showSuccessToast('验证码验证成功')
          router.push({ name: 'PasswordReset' })
        } else {
          showToast('验证失败,请输入正确验证码')
        }
      })
      .catch((err) => {
        console.error(err)
      })
  },
  onSendCode() {
    const reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
    if (!reg.test(mailbox.value)) {
      showToast('邮箱输入错误')
    } else {
      isClick.value = true
      const timer = setInterval(() => {
        num.value--

        if (num.value < 1) {
          clearInterval(timer)
          num.value = 60
          isClick.value = false
        }
      }, 1000)

      const email = mailbox.value
      postFormData({
        url: '/auth/password/code/send',
        params: {
          email: email
        }
      })
        .then((res) => {
          console.log({ res })
          showToast('验证码已发送,5分钟内有效')
        })
        .catch((err) => {
          console.error(err)
        })
    }
  },
  goLogin() {
    router.push({ name: 'Login' })
  },
  goLook() {
    router.push({ name: 'Home' })
  }
}
</script>

<style lang="less" scoped>
.head {
  .logo {
    display: flex;
    align-items: center;
    img {
      height: 36px;
    }
    .logo-title {
      font-size: 16px;
      font-weight: bold;
    }
  }
  .goBefore {
    color: rgb(12, 52, 186);
  }
}
.login-box {
  padding: 0 20px;
  margin-top: 30%;
  .login-title {
    color: #666;
  }
  .login-subTitle {
    color: #666;
    font-size: 16px;
  }
  .login-tip {
    text-align: right;
    font-size: 14px;
    line-height: 30px;
    margin-top: 10px;
    margin-bottom: 40px;
    padding-right: 10px;
  }
  .popup-title {
    color: #666;
    font-size: 24px;
    padding: 10px;
    font-weight: bold;
  }
}
:deep(.van-field__label) {
  margin: 0;
}
</style>
